﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using Microsoft.Win32;

namespace MyWcfRestClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        WebClient uploadWc = new WebClient();
        private void buttonUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "jpg files(*.jpg)|*.jpg";
            if (true == op.ShowDialog())
            {
                uploadWc.Headers.Add("Content-Type", "image/jpeg");
                System.IO.FileStream fs = new System.IO.FileStream(op.FileName, System.IO.FileMode.Open);
                byte[] buffer = new byte[(int)fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                fs.Close();
                uploadWc.UploadDataCompleted += new System.Net.UploadDataCompletedEventHandler(UploadDataCompleted);
                try
                {
                    uploadWc.UploadDataAsync(new Uri("http://localhost:8231/Picture/Upload"), buffer);
                }
                catch (WebException webException)
                {
                    string message = webException.Message;
                    Debug.Print(message);
                }
                buttonUpload.IsEnabled = false;
                buttonCancelUpload.IsEnabled = true;
            }
        }

        void UploadDataCompleted(object sender, System.Net.UploadDataCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                buttonUpload.IsEnabled = true;
                buttonCancelUpload.IsEnabled = false;
                MessageBox.Show("Upload cancelled!");
                return;
            }
            MessageBox.Show("Upload completed!");
            buttonUpload.IsEnabled = true;
            buttonCancelUpload.IsEnabled = false;
        }

        WebClient downloadClient = new WebClient();
        private void buttonDownload_Click(object sender, RoutedEventArgs e)
        {
            downloadClient = new System.Net.WebClient();

            SaveFileDialog op = new SaveFileDialog();
            if (true == op.ShowDialog())
            {
                downloadClient.DownloadFileAsync(new Uri("http://127.0.0.1:8231/Picture/Download"), op.FileName);
                downloadClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
                buttonDownload.IsEnabled = true;
                buttonCancelDownload.IsEnabled = false;
            }
        }

        void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                buttonCancelUpload.IsEnabled = false;
                buttonUpload.IsEnabled = true;
                MessageBox.Show("Download Cancelled!");
                return;
            }
            using (System.Net.WebClient wc = e.UserState as System.Net.WebClient)
            {
                buttonCancelUpload.IsEnabled = false;
                buttonUpload.IsEnabled = true;
                MessageBox.Show("Download completed!");
            }
        }

    }
}
